package com.xiaoma.service.impl;

import com.xiaoma.mapper.SoftwareCategoryMapper;
import com.xiaoma.mapper.SoftwareListMapper;
import com.xiaoma.pojo.Software;
import com.xiaoma.service.SoftwareListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class SoftwareListServiceImpl implements SoftwareListService {

    @Autowired
    private SoftwareListMapper softwareListMapper;
    @Autowired
    private SoftwareCategoryMapper softwareCategoryMapper;

    /**
     * 添加软件
     * @param software
     */
    @Transactional
    @Override
    public void addSoftware(Software software) {
        software.setCreateTime(LocalDateTime.now());
        software.setUpdateTime(LocalDateTime.now());
        softwareListMapper.insertSoftware(software);
        List<Integer> categoryIds = software.getCategoryId();
        log.info("获取到前端请求的软件id:{}",software.getId());
        log.info("获取到前端请求的软件分类集合:{}", categoryIds);
        if (!categoryIds.isEmpty()){
            softwareCategoryMapper.insertSoftwareCatgory(software.getId(),categoryIds);
        }

    }

    /**
     * 删除软件
     * @param id
     */
    @Override
    public void deleteSoftware(Integer id) {
        softwareListMapper.deleteSoftware(id);
    }

    /**
     * 修改软件
     */
    @Override
    public void updateSoftware(Software software){
        software.setUpdateTime(LocalDateTime.now());
        softwareListMapper.updateSoftware(software);
        //判断categoryId是否不同，如果不同则删除在添加实现修改功能
        List<Integer> categoryIds = software.getCategoryId(); // 获取categoryId(前端请求的)
        log.info("前端请求的categoryIds:{}", categoryIds);
        List<Integer> softwareCategoryId = softwareCategoryMapper.getSoftwareCategoryId(software.getId()); // 获取categoryId(数据库中的)
        if (!categoryIds.isEmpty() && !categoryIds.equals(softwareCategoryId)){
            softwareCategoryMapper.deleteSoftwareCategory(software.getId());
            softwareCategoryMapper.insertSoftwareCatgory(software.getId(), categoryIds);
        }
    }

    /**
     * 查询操作（可以含有多个categoryId）
     * @return
     */
    @Override
    public List<Software> getSoftwareList() {
        List<Software> softwareList = softwareListMapper.getSoftwareList();
        for (Software software : softwareList) {
            Integer softwareId = software.getId();
            software.setCategoryId(softwareCategoryMapper.getSoftwareCategoryId(softwareId));
        }
        return softwareList;
    }

    /**
     * 根据id进行软件信息的回显
     * @param id
     * @return
     */
    @Override
    public Software getSoftwareById(Integer id) {
        Software softwareById = softwareListMapper.getSoftwareById(id);
        List<Integer> softwareCategoryId = softwareCategoryMapper.getSoftwareCategoryId(id);
        softwareById.setCategoryId(softwareCategoryId);
        return softwareById;
    }
}
